/**
 * Copyright (c) 2018-2022, NXOS Development Team
 * SPDX-License-Identifier: Apache-2.0
 * 
 * Contains: io quque test 
 * 
 * Change Logs:
 * Date           Author            Notes
 * 2023-02-06     JasonHu           Init
 */

#include <test/utest.h>
#include <base/ioqueue.h>

#ifdef CONFIG_NX_UTEST_IO_QUEUE

NX_TEST(NX_IoQueueCreate)
{
    NX_Error err;
    NX_IoQueue * queue;
    
    queue = NX_IoQueueCreate(0, &err);
    NX_ASSERT_TRUE(queue == NX_NULL && err != NX_EOK);
    queue = NX_IoQueueCreate(NX_IOQUEUE_MAX_LEN + 1, &err);
    NX_ASSERT_TRUE(queue == NX_NULL && err != NX_EOK);
    queue = NX_IoQueueCreate(32, &err);
    NX_ASSERT_TRUE(queue != NX_NULL && err == NX_EOK);
    NX_IoQueueDestroy(queue);
}

NX_TEST(NX_IoQueueDestroy)
{
    NX_Error err;
    NX_IoQueue * queue = NX_NULL;
    
    err = NX_IoQueueDestroy(queue);
    NX_ASSERT_NE(err, NX_EOK);

    queue = NX_IoQueueCreate(32, &err);
    NX_ASSERT_TRUE(queue != NX_NULL && err == NX_EOK);
    err = NX_IoQueueDestroy(queue);
    NX_ASSERT_EQ(err, NX_EOK);
}

NX_TEST(NX_IoQueueTransmit)
{
    NX_Error err;
    NX_IoQueue * queue;
    char ch;
    int i;
    
    queue = NX_IoQueueCreate(32, &err);
    NX_ASSERT_TRUE(queue != NX_NULL && err == NX_EOK);

    ch = NX_IoQueueGet(NX_NULL, &err);
    NX_ASSERT_TRUE(ch == 0 && err != NX_EOK);

    err = NX_IoQueuePut(NX_NULL, 'A');
    NX_ASSERT_TRUE(err != NX_EOK);

    err = NX_IoQueuePut(queue, 'A');
    NX_ASSERT_TRUE(queue != NX_NULL && err == NX_EOK);

    ch = NX_IoQueueGet(queue, &err);
    NX_ASSERT_TRUE(ch == 'A');

    for (i = 0; i < 32; i++)
    {
        err = NX_IoQueuePut(queue, (char)i);
        NX_ASSERT_TRUE(queue != NX_NULL && err == NX_EOK);
    }

    for (i = 0; i < 32; i++)
    {
        ch = NX_IoQueueGet(queue, &err);
        NX_ASSERT_TRUE(ch == (char)i);
    }

    err = NX_IoQueueDestroy(queue);
    NX_ASSERT_EQ(err, NX_EOK);
}

NX_TEST(NX_IoQueueTryTransmit)
{
    NX_Error err;
    NX_IoQueue * queue;
    char ch;
    int i;
    
    queue = NX_IoQueueCreate(32, &err);
    NX_ASSERT_TRUE(queue != NX_NULL && err == NX_EOK);

    ch = NX_IoQueueTryGet(NX_NULL, &err);
    NX_ASSERT_TRUE(ch == 0);

    err = NX_IoQueueTryPut(NX_NULL, 'A');
    NX_ASSERT_TRUE(err != NX_EOK);

    err = NX_IoQueueTryPut(queue, 'A');
    NX_ASSERT_TRUE(queue != NX_NULL && err == NX_EOK);

    ch = NX_IoQueueTryGet(queue, &err);
    NX_ASSERT_TRUE(ch == 'A');

    ch = NX_IoQueueTryGet(queue, &err);
    NX_ASSERT_TRUE(ch == 0);

    for (i = 0; i < 32; i++)
    {
        err = NX_IoQueueTryPut(queue, (char)i);
        NX_ASSERT_TRUE(queue != NX_NULL && err == NX_EOK);
    }
    
    err = NX_IoQueueTryPut(queue, '#');
    NX_ASSERT_TRUE(err != NX_EOK);


    for (i = 0; i < 32; i++)
    {
        ch = NX_IoQueueTryGet(queue, &err);
        NX_ASSERT_TRUE(ch == (char)i);
    }

    err = NX_IoQueueDestroy(queue);
    NX_ASSERT_EQ(err, NX_EOK);
}

NX_TEST_TABLE(NX_IoQueue)
{
    NX_TEST_UNIT(NX_IoQueueCreate),
    NX_TEST_UNIT(NX_IoQueueDestroy),
    NX_TEST_UNIT(NX_IoQueueTransmit),
    NX_TEST_UNIT(NX_IoQueueTryTransmit),
};

NX_TEST_CASE(NX_IoQueue);

#endif
